//
//  AddProdToCartUtil.m
//  JiaoX
//
//  Created by jiaoxiu－005 on 15/1/27.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "AddProdToCartUtil.h"
#import "CartDataTool.h"
@implementation AddProdToCartUtil

+(void)postProdToServer:(UIView *)container productDetail:(ProductDetail *)productDetail selectSkuPosition:(NSUInteger)selectSkuPosition prodCount:(NSUInteger)prodCount ctrl:(UIViewController *)ctrl
{
    ProdSub *prodSub = productDetail.prodSub[selectSkuPosition]; 
    CartProdSub *cartProdSub = [[CartProdSub alloc]init];
    [cartProdSub setProdMainId: [NSString stringWithFormat:@"%d",productDetail.prodId]];
    [cartProdSub setProdId:[NSString stringWithFormat:@"%d",prodSub.proSubId]];
    [cartProdSub setProdBuyLink:prodSub.prodBuyLink];
    [cartProdSub setProdImageUrl:productDetail.prodImages[0]];
    [cartProdSub setProdName:productDetail.prodName];
    [cartProdSub setProdOldPrice:[NSString stringWithFormat:@"%.0f",productDetail.prodOldPrice]];
    [cartProdSub setProdNewPrice:[NSString stringWithFormat:@"%.0f",productDetail.prodNewPrice]];
    [cartProdSub setProdCount:[NSString stringWithFormat:@"%lu",(unsigned long)prodCount]];
    
    JXBaseRequest *request = [[JXBaseRequest alloc]init];
    NSDictionary *dic = [request keyValues];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:dic];
    [params setObject:cartProdSub.prodId forKey:@"prodId"];
    [params setObject:cartProdSub.prodCount forKey:@"prodCount"];
    
    MBProgressHUD *HUD = [DialogUtils showLoadingWithView:container];
    //block中得东东一定使用弱引用
    __weak UIViewController *weak_ctrl = ctrl;
    __weak MBProgressHUD *hud = HUD;
    [CartDataTool addCartProdWithParam:params success:^(CartInfo *result) {
        [hud hide:YES];
        if ([Utils isBlankString:result.data]) {
            [weak_ctrl performSelector:@selector(commitSuccess)];
        }else{
            [DialogUtils showTipsWithView:container String:result.data];
        }
    } failure:^(NSError *error) {
        [hud hide:YES];
        [weak_ctrl performSelector:@selector(commitFailure)];
    }];
}

+(BOOL)assertAddCart:(UIView *)container productDetail:(ProductDetail *)productDetail dao:(CartInfoDao *)dao addNumber:(NSUInteger)addNumber selectSkuPosition:(NSUInteger)selectSkuPosition
{
    BOOL result = YES;
    if(productDetail.prodNewPrice == 0)
    {
        if([self isMoreThan:productDetail dao:dao count:addNumber maxValue:1 selectSkuPosition:selectSkuPosition])
        {// 判断零元商品
            [DialogUtils showTipsWithView:container String:@"0元商品限1件"];
            result = NO;
        }
    }else if(productDetail.prodPromotion&& ![Utils isBlankString:productDetail.prodPromotion.price])
    {
        if([self isMoreThan:productDetail dao:dao count:addNumber maxValue:productDetail.prodPromotion.limitCount selectSkuPosition:selectSkuPosition])
        {// 限购商品
            [DialogUtils showTipsWithView:container String:[NSString stringWithFormat:@"抢购品限%d件",productDetail.prodPromotion.limitCount]];
            result = NO;
        }
    }else if([self isMoreThan:productDetail dao:dao count:addNumber maxValue:20 selectSkuPosition:selectSkuPosition])
    {
        [DialogUtils showTipsWithView:container String: @"单件商品最多选择20件"];
        result = NO;
        
    }
    return result;
}

+(BOOL)isMoreThan:(ProductDetail *)productDetail dao:(CartInfoDao *)dao count:(NSUInteger)count maxValue:(int)maxValue selectSkuPosition:(NSUInteger) selectSkuPosition
{
    ProdSub *prodSub = productDetail.prodSub[selectSkuPosition];
    int queryCount = [dao queryProdCount: [NSString stringWithFormat:@"%d",prodSub.proSubId]];
    return count + queryCount > maxValue;
}

+ (void)addProdToDb:(ProdSub *)prodSub prodDetail:(ProductDetail *)prodDetail dao:(CartInfoDao *)cartDao prodCount:(NSUInteger)count{
    // 往数据库里面添加数据 1.增加买赠商品
    
    CartProdSub *cartProdSub = [[CartProdSub alloc]init];
    [cartProdSub setProdMainId: [NSString stringWithFormat:@"%d",prodDetail.prodId]];
    [cartProdSub setProdId:[NSString stringWithFormat:@"%d",prodSub.proSubId]];
    [cartProdSub setProdBuyLink:prodSub.prodBuyLink];
    [cartProdSub setProdImageUrl:prodDetail.prodImages[0]];
    [cartProdSub setProdName:prodDetail.prodName];
    [cartProdSub setProdOldPrice:[NSString stringWithFormat:@"%.0f",prodDetail.prodOldPrice]];
    [cartProdSub setProdNewPrice:[NSString stringWithFormat:@"%.0f",prodDetail.prodNewPrice]];
    [cartProdSub setProdCount:[NSString stringWithFormat:@"%lu",(unsigned long)count]];
    [cartProdSub setProdPromotion:prodDetail.prodPromotion];
    
    // 判断购物车是否已经有相同ID的商品数量,如果没有直接添加，如果有，去除原有数量与现在数量相加再赋值存储
    int queryCount = [cartDao queryProdCount:cartProdSub.prodId];
    
    if (queryCount == 0) {
        [cartDao addProd:cartProdSub.prodId cartProd:[cartProdSub keyValues]];
    } else {
        int count = (int)[cartProdSub.prodCount integerValue] + queryCount;
        [cartProdSub setProdCount:[NSString stringWithFormat:@"%d",count]];
        [cartDao updateProd:cartProdSub.prodId cartProd:[cartProdSub keyValues]];
    }
}

@end
